package ua.exchangeratesukraine.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;

import ua.exchangeratesukraine.R;
import ua.exchangeratesukraine.model.CurrencyModel;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BanksAdapter extends BaseAdapter {

	private final String				LOG_TAG			= getClass().getSimpleName();
	private Context						context;
	private ArrayList<CurrencyModel>	currencyModelArray;
	private CurrencyModel				currencyModel	= null;

	// For view recycling (optimization for listview)
	static class ViewHolder {
		TextView	tvNameBanks;
		TextView	tvBuy;
		TextView	tvBuyChangeAbs;
		TextView	tvBuyChangeRel;
		TextView	tvSale;
		TextView	tvSaleChangeAbs;
		TextView	tvSaleChangeRel;
	}

	public BanksAdapter(Context context, ArrayList<CurrencyModel> currencyModel, 
						String[] bankCodes, String[] bankNames) {
		this.context = context;
		this.currencyModelArray = currencyModel;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.list_item_banks, null);
			holder.tvNameBanks = (TextView) view.findViewById(R.id.tvNameBanks);
			holder.tvBuy = (TextView) view.findViewById(R.id.tvBuy);
			holder.tvBuyChangeAbs = (TextView) view.findViewById(R.id.tvBuyChangeAbs);
			holder.tvBuyChangeRel = (TextView) view.findViewById(R.id.tvBuyChangeRel);
			holder.tvSale = (TextView) view.findViewById(R.id.tvSale);
			holder.tvSaleChangeAbs = (TextView) view.findViewById(R.id.tvSaleChangeAbs);
			holder.tvSaleChangeRel = (TextView) view.findViewById(R.id.tvSaleChangeRel);
			view.setTag(holder);
		}
		if (position % 2 != 0) {
			view.setBackgroundResource(R.color.listview_item_2);
		} else {
			view.setBackgroundResource(R.color.listview_item_1);
		}

		currencyModel = ((CurrencyModel) getItem(position));

//		Log.d(LOG_TAG, "Locale = " + Locale.getDefault().toString());

		
		if (currencyModel != null) {
			holder = (ViewHolder) view.getTag();

			holder.tvNameBanks.setText(currencyModel.getName());
			holder.tvBuy.setText(currencyModel.getBuy());
			holder.tvBuyChangeAbs.setText(currencyModel.getBuyChangeAbs());
			holder.tvBuyChangeRel.setText(currencyModel.getBuyChangeRel());
			holder.tvSale.setText(currencyModel.getSale());
			holder.tvSaleChangeAbs.setText(currencyModel.getSaleChangeAbs());
			holder.tvSaleChangeRel.setText(currencyModel.getSaleChangeRel());
		}
		return view;
	}

	@Override
	public int getCount() {
		return currencyModelArray.size();
	}

	@Override
	public Object getItem(int position) {
		return currencyModelArray.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public ArrayList<CurrencyModel> getCurrencyModelArray() {
		return currencyModelArray;
	}
}